package pe.com.casadesign.service.util;

import java.util.Enumeration;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SesionUtil
{
    private static final Logger logger = LoggerFactory.getLogger(SesionUtil.class);

    @SuppressWarnings("unchecked")
    public static <ClaseGenerica> ClaseGenerica obtenerObjectoDeSesion(HttpSession sesion,
            VariablesSession variablesSession, Class<ClaseGenerica> claseGenerica)
    {
        Object objetoSesion = sesion.getAttribute(variablesSession.toString());
        return (ClaseGenerica) objetoSesion;
    }

    public static void guardarObjetoEnSession(HttpSession sesion, VariablesSession variablesSession, Object objeto)
    {
        if (objeto != null)
        {
            sesion.setAttribute(variablesSession.toString(), objeto);
        }
    }

    public static boolean estaObjetoEnSesion(HttpSession sesion, VariablesSession variablesSession)
    {
        return sesion.getAttribute(variablesSession.toString()) != null;
    }

    @SuppressWarnings("rawtypes")
	public static void eliminarObjectoDeSesion(HttpSession sesion, VariablesSession variablesSession)
    {
        logger.debug("eliminarObjectoDeSesion");
        sesion.removeAttribute(variablesSession.toString());
        if (logger.isDebugEnabled())
        {
            logger.debug("objectoEliminado=" + variablesSession.toString());
            Enumeration atributosEnSesion = sesion.getAttributeNames();
            String nombreAtributo;
            while (atributosEnSesion != null && atributosEnSesion.hasMoreElements())
            {
                nombreAtributo = atributosEnSesion.nextElement().toString();
                Object objetoEnSesion = sesion.getAttribute(nombreAtributo);
                if (objetoEnSesion != null)
                {
                    if (logger.isDebugEnabled())
                    {
                        logger.debug(nombreAtributo + ": " + ToStringBuilder.reflectionToString(objetoEnSesion));
                    }
                }
            }
        }
    }

    @SuppressWarnings("unchecked")
    public static void limpiarSesion(HttpSession sesion)
    {
        String tipoAplicacionCliente = (String) sesion.getAttribute(VariablesSession.TIPO_APLICACION_CLIENTE.toString());
        Enumeration<String> nombresAtributos = sesion.getAttributeNames();
        while (nombresAtributos != null && nombresAtributos.hasMoreElements())
        {
            String atributo = nombresAtributos.nextElement();

            if (//!VariablesSession.USUARIO_COMPLETO_BEAN.toString().equals(atributo)&& 
            	  !VariablesSession.IP_APLICATIVO_CLIENTE.toString().equals(atributo)&&
            	  !VariablesSession.TIPO_APLICACION_CLIENTE.toString().equals(atributo))
            {
                if (logger.isDebugEnabled())
                {
                    logger.debug("se limpia el atributo" + atributo);
                }
                sesion.removeAttribute(atributo);
            }
        }
    }

}